package com.uenpay.util.imageloader;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;

import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.DrawableTypeRequest;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.uenpay.util.common.ACache;
import com.uenpay.util.imageloader.transform.GlideCircleTransform;
import com.uenpay.util.imageloader.transform.GlideRoundTransform;

/**
 * Created by liulou on 2016/11/17.
 * <p>封装第三方图片库Glide</p>
 * <p>已实现封装：</p>
 * <p>1. 可以传Activity，Fragment，Context等不同的context参数，让图片加载与context生命周期一致（Glide特色功能）</p>
 * <p>2.占位符图片</p>
 * <p>3.图片地址类型支持url、uri、drawable</p>
 * <p>4.支持圆形图片、圆角图片</p>
 */

public class GlideImageLoaderStrategy implements BaseImageLoaderStrategy {

    @Override
    public void loadImage(Object context, ImageLoader img) {
        Context mContext;
        RequestManager requestManager = null;
        if (context instanceof Activity) {
            requestManager = Glide.with((Activity) context);
            mContext = (Context) context;
        } else if (context instanceof Fragment) {
            requestManager = Glide.with((Fragment) context);
            mContext = ((Fragment) context).getActivity();
        } else if (context instanceof android.support.v4.app.Fragment) {
            requestManager = Glide.with((android.support.v4.app.Fragment) context);
            mContext = ((Fragment) context).getActivity();
        } else if (context instanceof FragmentActivity) {
            requestManager = Glide.with((FragmentActivity) context);
            mContext = (FragmentActivity) context;
        } else if (context instanceof Context) {
            requestManager = Glide.with((Context) context);
            mContext = (Context) context;
        } else {
            throw new IllegalArgumentException("Context arg is invid");
        }
        DrawableTypeRequest drawableTypeRequest = null;

        if (img.getResId() != 0) {
            drawableTypeRequest = requestManager.load(img.getResId());
        } else if (!TextUtils.isEmpty(img.getUrl())) {
            drawableTypeRequest = requestManager.load(img.getUrl());
        } else if (img.getUri() != null) {
            drawableTypeRequest = requestManager.load(img.getUri());
        } else {
            throw new IllegalArgumentException("set image load address!");
        }

        if (img.isCache()) {
            drawableTypeRequest.skipMemoryCache(false);
        } else {
            drawableTypeRequest.skipMemoryCache(true);
            drawableTypeRequest.diskCacheStrategy(DiskCacheStrategy.NONE);
        }

        DrawableRequestBuilder drawableRequestBuilder = null;
        if (img.isCircle()) {
            if (mContext == null) {
                return;
            }
            drawableRequestBuilder = drawableTypeRequest.transform(new GlideCircleTransform(mContext));
        } else if (img.getRoundRadius() > 0) {
            drawableRequestBuilder = drawableTypeRequest.transform(new GlideRoundTransform(mContext, img.getRoundRadius()));
        } else {

        }

        if (drawableRequestBuilder == null) {
            drawableTypeRequest.placeholder(img.getPlaceHolder()).into(img.getImgView());
        } else {
            drawableRequestBuilder.placeholder(img.getPlaceHolder()).into(img.getImgView());
        }

    }

    @Override
    public void asBitmap(Object context, final ImageLoader img) {
        Context mContext;
        RequestManager requestManager = null;
        if (context instanceof Activity) {
            requestManager = Glide.with((Activity) context);
            mContext = (Context) context;
        } else if (context instanceof Fragment) {
            requestManager = Glide.with((Fragment) context);
            mContext = ((Fragment) context).getActivity();
        } else if (context instanceof android.support.v4.app.Fragment) {
            requestManager = Glide.with((android.support.v4.app.Fragment) context);
            mContext = ((Fragment) context).getActivity();
        } else if (context instanceof FragmentActivity) {
            requestManager = Glide.with((FragmentActivity) context);
            mContext = (FragmentActivity) context;
        } else if (context instanceof Context) {
            requestManager = Glide.with((Context) context);
            mContext = (Context) context;
        } else {
            throw new IllegalArgumentException("Context arg is invid");
        }

        DrawableTypeRequest drawableTypeRequest = null;

        if (img.getResId() != 0) {
            drawableTypeRequest = requestManager.load(img.getResId());
        } else if (!TextUtils.isEmpty(img.getUrl())) {
            drawableTypeRequest = requestManager.load(img.getUrl());
        } else if (img.getUri() != null) {
            drawableTypeRequest = requestManager.load(img.getUri());
        } else {
            throw new IllegalArgumentException("set image load address!");
        }

        if (img.isCache()) {
            drawableTypeRequest.skipMemoryCache(false);
        } else {
            drawableTypeRequest.skipMemoryCache(true);
            drawableTypeRequest.diskCacheStrategy(DiskCacheStrategy.NONE);
        }
        final ACache aCache = ACache.get(mContext);
        drawableTypeRequest.into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                aCache.put(img.getKey(), resource);
            }
        });

    }
}
